package com.iot.inter.controller.newinterface.sync;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.newpojo.response.subResponse.SyncPackageAndDeviceResponse;
import com.iot.inter.entity.newpojo.response.subResponse.common.Page;
import com.iot.inter.entity.newpojo.response.subResponse.common._DevicePackages;
import com.iot.inter.entity.newpojo.response.subResponse.common._PackageCodeList;
import com.iot.inter.entity.newpojo.response.subResponse.common._PackagesDevice;
import com.iot.inter.entity.newrequest.sync.syncrelation.request.RequestSyncDevicePackages;
import com.iot.inter.entity.newrequest.sync.syncrelation.response.ResponseDevicePackages;
import com.iot.inter.entity.newrequest.sync.syncrelation.response.ResponsePackagesDevices;
import com.iot.inter.entity.newrequest.sync.syncrelation.response.ResponseSyncDevicePackages;
import com.iot.inter.entity.newrequest.sync.syncrelation.response.ResponseSyncDevicePackagesList;
import com.iot.inter.interfaceprocessing.newinterface.TrunToSyncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 去北京做：3.24 批量同步设备可订购套餐：2.0接口
 * 该接口主要提供给领科内部系统使用，用来批量获取指定客户的设备可订购套餐对应关系信息。
 *
 * @author lm
 * @date 2019/8/29
 */
@RestController
@Api(description = "3.24 批量同步设备可订购套餐")
public class SyncDeviceAndPackageRelationController {

    private static Logger logger = Logger.getLogger(SyncDeviceAndPackageRelationController.class);

    /**
     * 考虑到该接入方使用的资费套餐可能有很多，所以每次同步时，都采用分页的方式，每次仅同步给接入方一页的资费套餐信息，
     * 每页包含的资费套餐信息个数由接入方自行定义。
     * 如果接入方想获取所有资费套餐信息，并且存在多页套餐信息的话，接入方需要多次调用同步资费套餐接口。
     *
     * @author lm
     * @date 2019/8/26
     */

    @CrossOrigin
    @RequestMapping(value = "syncDevicePackages", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.24 批量同步设备可订购套餐", response = ResponseSyncDevicePackages.class)
    public String syncDevicePackages(@ApiParam @RequestBody RequestSyncDevicePackages request) {
        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);
        if (requestStr != null) {

            logger.info("【批量同步设备可订购套餐】请求：" + requestStr);

            RequestSyncDevicePackages requestPackage = JSONArray.parseObject(requestStr, RequestSyncDevicePackages.class);
            Integer pageNo = requestPackage.getPageNo();
            Integer pageSize = requestPackage.getPageSize();
            String partnerCode = requestPackage.getPartnerCode();

            TrunToSyncService sync = new TrunToSyncService();
            SyncPackageAndDeviceResponse response = sync.syncDevicePackages(pageNo, pageSize, partnerCode);

            List<ResponseSyncDevicePackagesList> tradeDataList = new ArrayList<ResponseSyncDevicePackagesList>();

            if (response != null) {

                ResponseSyncDevicePackagesList tradeData = null;

                Page page = null;

                if (response.getBody() != null) {

                    List<ResponseDevicePackages> dpList = new ArrayList<ResponseDevicePackages>();

                    if (response.getBody().getDevice_packages() != null) {

                        ResponseDevicePackages rdp = null;

                        for (_DevicePackages dp : response.getBody().getDevice_packages()) {

                            List<ResponsePackagesDevices> pdList = new ArrayList<ResponsePackagesDevices>();

                            if (dp.getPackages() != null && StringUtils.isNotBlank(dp.getDeviceId())) {

                                for (_PackagesDevice pd : dp.getPackages()) {

                                    if (pd.getPackage_list() != null) {

                                        for (_PackageCodeList pCodeList : pd.getPackage_list()) {

                                            ResponsePackagesDevices rpd = null;

                                            if (pCodeList.getPackageCode() != null) {

                                                rpd = new ResponsePackagesDevices();

                                                rpd.setPackageCode(pCodeList.getPackageCode());
                                                rpd.setPackageNum(pd.getPackageNum());
                                                pdList.add(rpd);

                                            }

                                        }
                                    }

                                }

                            }

                            rdp = new ResponseDevicePackages();
                            rdp.setDeviceId(dp.getDeviceId());
                            rdp.setPackages(pdList);
                            dpList.add(rdp);

                        }

                        tradeData = new ResponseSyncDevicePackagesList();
                        tradeData.setDevice_packages(dpList);
                        tradeDataList.add(tradeData);

                        page = new Page();
                        page.setCurrentPageNo(response.getBody().getPage().getCurrentPageNo());
                        page.setPageSize(response.getBody().getPage().getPageSize());
                        page.setTotalCount(response.getBody().getPage().getTotalCount());
                        page.setTotalPages(response.getBody().getPage().getTotalPages());


                    }

                }

                ResponseSyncDevicePackages responseOrder = new ResponseSyncDevicePackages();
                responseOrder.setDevicePackagesNum(response.getBody().getDeviceNum());
                responseOrder.setTradeData(tradeDataList);
                responseOrder.setPage(page);
                responseOrder.setRetCode(response.getRetCode());
                responseOrder.setResultInfo(response.getResultInfo());

                jsonString = JSON.toJSONString(responseOrder);
                logger.info("【批量同步设备可订购套餐】成功响应：" + jsonString);
                return jsonString;


            } else {
                ResponseSyncDevicePackages responseOrder = new ResponseSyncDevicePackages();
                responseOrder.setRetCode("9998");
                responseOrder.setResultInfo("响应报文为空");
                jsonString = JSON.toJSONString(responseOrder);
                logger.info("【批量同步设备可订购套餐】响应：" + jsonString);
                return jsonString;
            }


        } else {

            ResponseSyncDevicePackages responseOrder = new ResponseSyncDevicePackages();
            responseOrder.setRetCode("9998");
            responseOrder.setResultInfo("请求报文为空");
            jsonString = JSON.toJSONString(responseOrder);
            logger.info("【批量同步设备可订购套餐】响应：" + jsonString);
            return jsonString;

        }

    }

}
